//
//  ConnectionModel.m
//  网络block
//
//  Created by 太阳 on 15/5/15.
//  Copyright (c) 2015年 三人. All rights reserved.
//

#import "ConnectionModel.h"

@interface ConnectionModel ()<NSURLConnectionDataDelegate>

@end

@implementation ConnectionModel



+ (void)connectDoneWithStr:(NSString *)str block:(void(^)(NSData *data))block
{
    ConnectionModel *model = [[ConnectionModel alloc]init];
    model.finishedBlock = block;
    [model starConnectWithStr:str];
}

- (void)starConnectWithStr:(NSString *)str
{
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    
    [NSURLConnection connectionWithRequest:request delegate:self];
    
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.receivedata = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receivedata appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.finishedBlock(self.receivedata);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"%@", error);
}
@end
